<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>聊天</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="../style.css">
</head>

<body>
  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

  <div id="app">
    <ele></ele>
  </div>

  <script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js'></script>
  <script src="" async defer></script>

  <script>
    Vue.component('ele', {
      render: function (createElement) {
        var _this = this
        // 渲染聊天内容列表
        if (this.list.length) {
          var listNode = createElement(
            'ul',
            this.list.map(
              function (item) {
                return createElement(
                  'li',
                  item
                )
              }
            )
          )
        } else {
          var listNode = createElement(
            'p',
            '暂无聊天内容'
          )
        }
        return createElement(
          'div',
          {
            class:{
              component: true
            }
          },
          [
            listNode,
            createElement(
              'input', {
                style: {
                  width: '200px',
                },
                attrs: {
                  placeholder: '请输入内容，按回车健发送'
                },
                on: {
                  keyup: function (event) {
                    //检查是否回车
                    if (event.keyCode !== 13) return
                    //添加输入的内容到列表
                    _this.list.push(event.target.value)
                    //发送后，清空输入框
                    event.target.value = ''
                  }
                }
              }
            )
          ]
        )
      },
      data: function () {
        return {
          value: '',
          list: []
        }
      }
    })

    var app = new Vue({
      el: '#app'
    })
  </script>
</body>

</html>